package com.cmict.data.system.dal.mysql.portal;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.query.QueryWrapperX;
import com.cmict.data.system.controller.admin.portal.vo.PortalExportReqVO;
import com.cmict.data.system.controller.admin.portal.vo.PortalPageReqVO;
import com.cmict.data.system.dal.dataobject.portal.PortalDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 门户 Mapper
 *
 * @author awesome
 */
@Mapper
public interface PortalMapper extends BaseMapperX<PortalDO> {

    default PageResult<PortalDO> selectPage(PortalPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PortalDO>()
                .eqIfPresent(PortalDO::getPortalNumber, reqVO.getPortalNumber())
                .likeIfPresent(PortalDO::getPortalName, reqVO.getPortalName())
                .eqIfPresent(PortalDO::getPortalJsonMessage, reqVO.getPortalJsonMessage())
                .eqIfPresent(PortalDO::getPortalDesc, reqVO.getPortalDesc())
                .eqIfPresent(PortalDO::getManageMenuId, reqVO.getManageMenuId())
                .eqIfPresent(PortalDO::getIsEnable, reqVO.getIsEnable())
                .eqIfPresent(PortalDO::getIsReference, reqVO.getIsReference())
                .betweenIfPresent(PortalDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(PortalDO::getId));
    }

    default List<PortalDO> selectList(PortalExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<PortalDO>()
                .eqIfPresent(PortalDO::getPortalNumber, reqVO.getPortalNumber())
                .likeIfPresent(PortalDO::getPortalName, reqVO.getPortalName())
                .eqIfPresent(PortalDO::getPortalJsonMessage, reqVO.getPortalJsonMessage())
                .eqIfPresent(PortalDO::getPortalDesc, reqVO.getPortalDesc())
                .eqIfPresent(PortalDO::getManageMenuId, reqVO.getManageMenuId())
                .eqIfPresent(PortalDO::getIsEnable, reqVO.getIsEnable())
                .eqIfPresent(PortalDO::getIsReference, reqVO.getIsReference())
                .betweenIfPresent(PortalDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(PortalDO::getId));
    }

    default PortalDO selectOneManageMenuId(Long id) {
        return selectOne(new LambdaQueryWrapperX<PortalDO>()
                .eqIfPresent(PortalDO::getManageMenuId, id));
    }

    default PageResult<PortalDO> selectPageNoRelease(PortalPageReqVO reqVO){
        if(ObjectUtil.isNotEmpty(reqVO.getSource())){
            return selectPage(reqVO, new QueryWrapperX<PortalDO>()
                    .likeIfPresent("portal_name", reqVO.getPortalName())
                    .eq("source",reqVO.getSource())
                    .eq("is_enable",0)
                    .orderByDesc("create_time"));
        }else{
            return selectPage(reqVO, new QueryWrapperX<PortalDO>()
                    .likeIfPresent("portal_name", reqVO.getPortalName())
                    .isNull(true, "source")
                    .eq("is_enable",0)
                    .orderByDesc("create_time"));
        }
    }

    @Select("${sql}")
    @DS("slave")
    List<Map<String,Object>> getTableNameList(String sql);

    @Select("${sql}")
    @DS("slave")
    BigDecimal getFunData(String sql);

    @Select("${sql}")
    @DS("slave")
    List<String> getFieldData(String sql);

    @Select("${sql}")
    @DS("slave")
    String getFieldDataByDashboard(String sql);

    @Select("${sql}")
    Map<Object, Object> getMoveColumn(String sql);

    @Select("${sql}")
    Map<Object, Object> getMovePortal(String sql);

    @Select("${sql}")
    void inMoveData(String sql);
}
